import { export_json_to_excel } from '../excel/export2Excel';
export default {
  methods: {
    /**
     * @desc 表格导出
     * @param {Array<any>} list 表格数据
     * @param {Object} options 导出设置
     */
    exportData(list, options) {
      let columns = [...this.columns].filter((item) =>
        item.export !== undefined ? item.export : true
      );
      let header = [columns.map((item) => item.title)];
      let body = list.map((item) => {
        let obj = [];
        columns.forEach((column) => {
          obj.push(item[column.key]);
        });
        return obj;
      });
      let data = [body];
      let { filename, sheetname, bookType, autoWidth } = options;
      sheetname = [sheetname || 'sheet1'];
      filename = filename || 'filename';
      autoWidth = autoWidth || false;
      bookType = bookType || 'xlsx';
      let json = {
        header,
        data,
        sheetname,
        filename,
        autoWidth,
        bookType,
      };
      export_json_to_excel(json);
    },
  },
};
